package farmacia.dao.endereco;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import conexao.CriaConexao;



import farmacia.dao.endereco.email.Email;


public class EmailDao {
	private Connection conexao;

    public EmailDao() throws SQLException {
        this.conexao = CriaConexao.getConexao();

    }
             // Metodo de inserção em banco de dados mysql
    public void adicionaEmail(Email c1) throws SQLException {
        String sql = "insert  into tb_eMail("
                + "fi_eMail) values(?)";
        
        PreparedStatement stmt = conexao.prepareStatement(sql);

        stmt.setString(1, c1.getFi_eMail());
        
        stmt.executeUpdate();
        stmt.close();

    }

    public void excluirEmail(Email c1) throws SQLException {
        
        String sql = "delete from tb_eMail where fi_idEmail = ?";
        
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        stmt.setInt(1, c1.getFi_idEmail());
        
        stmt.execute();
        stmt.close();
    }

    public void alterarEmail(Email c1) throws SQLException {
        String sql = "UPDATE tb_eMail SET "
                + "fi_eMail= ? "
                +"WHERE fi_idEmail=?";
        
        PreparedStatement stmt = conexao.prepareStatement(sql);
        
                  // setar valores
           
        stmt.setString(1, c1.getFi_eMail());     
        stmt.setInt(3, c1.getFi_idEmail());
        
        
        stmt.executeUpdate();
        stmt.close();

    }

    
    
    
  
    public List<Email> getLista() throws SQLException{
        
    String sql = "select eMail from tb_eMail";
    PreparedStatement stmt = this.conexao.prepareStatement(sql);
   // stmt.setString(1, email);
    ResultSet rs = stmt.executeQuery();
    
    List <Email> listaEmail= new ArrayList<Email>();
    
    while (rs.next()){
     
        Email c1 = new Email();
               // c1.setFi_idEmail(Integer.valueOf(rs.getInt("fi_pk_id")));
                c1.setFi_eMail(rs.getString(1));
                
                 
        listaEmail.add(c1);
    }    
    rs.close();
    stmt.close();
    
    return listaEmail;
     
    }
}
